package com.foogui.java8.optional;

import org.junit.Test;

import java.util.Optional;

public class MyTest {
    @Test
    public void TestFlatMap() {
        Person person = null;
        Optional<Person> optPerson = Optional.ofNullable(person);
        String name = optPerson.flatMap(Person::getCar)
                .flatMap(Car::getInsurance)
                .map(Insurance::getName)
                .orElse("haha");
        System.out.println(name);
    }
    @Test
    public void TestFilter() {
        Person person = null;
        Optional<Person> optPerson = Optional.ofNullable(person);
        int minAge=10;
        String res = optPerson.filter(p -> p.getAge() >= minAge)
                .flatMap(Person::getCar)
                .flatMap(Car::getInsurance)
                .map(Insurance::getName)
                .orElse("默认值");
        System.out.println(res);
    }
}
